package com.cs496.finalproject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;

@SuppressWarnings("serial")
public class FinalProjectServerServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world.  This is a get");
		}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		//resp.setContentType("text/plain");
		//resp.getWriter().println("Hello, world, this is a post");
		 
		 BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
	      
	      String user = br.readLine();
	      try {
			JSONObject jobject = new JSONObject(user);
			float val1 = Integer.parseInt(jobject.getString("val1"));
			float val2 = Integer.parseInt(jobject.getString("val2"));
			float val3 = val1*val2/(val1 + val2);
			addItemToDatastore(val1, val2, val3);
			resp.setStatus(HttpServletResponse.SC_OK);
			resp.setContentType("text/plain");
			
			
			
	
			
			
			
			PrintWriter out = resp.getWriter();
			out.print("The parallel value of " + val1 +" ohms and "
			+val2+ " ohms is" + val3);

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	void addItemToDatastore(float a, float b, float c)
	{
		PersistenceManager pm = PMF.getPMF().getPersistenceManager(); 
		//This file will add items to the datastore.  picture are not introduced yet
			try { 
				
				Item item = new Item(a, b, c);
				pm.makePersistent(item);
			} 
			finally { 
			pm.close();
			}
		
	}
		
}
	
	
	
	

